#include "StdAfx.h"
#include "SubStructure.h"
#include "BulletFactory.h"
#include "EnemyFactory.h"
#include "PCBulletFactory.h"
#include "ItemFactory.h"
#include "EffectFactory.h"
#include "jsonreader.h"
#include "GameScene.h"
#include "Mediator.h"

Mediator::Mediator()
{
	EFactory = EnemyFactory::Instance();
	BFactory = BulletFactory::Instance();
	PBFactory = PCBulletFactory::Instance();
	IFactory = ItemFactory::Instance();
	EfFactory = EffectFactory::Instance();
}

void Mediator::SetScene(GameScene* nscene)
{
	scene = nscene;
	BFactory->SetScene(scene);
	PBFactory->SetScene(scene);
}

void Mediator::GetEvent(EventList Elist)
{
	EventList::iterator iter;
	EventList::iterator iter_end;
	iter = Elist.begin();
	iter_end = Elist.end();
	for(;iter != iter_end;iter++)
	{
		E_Queue.push_back(*iter);
	}
}
void Mediator::ProcessQueue()
{
	EventList enemyQueue;
	EventList bulletQueue;
	EventList PCQueue;
	EventList ItemQueue;
	EventList EffectQueue;
	EventList::iterator iter;
	EventList::iterator iter_end;
	iter = E_Queue.begin();
	iter_end = E_Queue.end();
	for(;iter != iter_end;iter++)
	{
		if((*iter)->ID == 3476)
		{
			enemyQueue.push_back(*iter);
		}
		else if((*iter)->ID == 0)
		{
			bulletQueue.push_back(*iter);
		}
		else if((*iter)->ID == 46)
		{
			PCQueue.push_back(*iter);
		}
		else if((*iter)->ID == 10101)
		{
			ItemQueue.push_back(*iter);
		}
		else if((*iter)->ID == 987)
		{
			DWORD tempflag = (*iter)->flag;
			if(tempflag == 1)
			{
				scene->OnEvent(this, MEDIATOR, STARTTALK, (*iter)->kind);
			}
		}
		else
		{
			EffectQueue.push_back(*iter);
		}
	}
	E_Queue.clear();
	MakeBullet(bulletQueue);
	MakeEnemy(enemyQueue);
	MakeUserBullet(PCQueue);
	MakeItem(ItemQueue);
	MakeEffect(EffectQueue);
}

void Mediator::MakeBullet(EventList Elist)
{
	EventList::iterator iter = Elist.begin();
	EventList::iterator iter_end = Elist.end();
	for(;iter != iter_end;iter++)
	{
		scene->PushBullet( BFactory->MakeBullet(*iter) );
	}
}
void Mediator::MakeEnemy(EventList Elist)
{
	EventList::iterator iter = Elist.begin();
	EventList::iterator iter_end = Elist.end();
	for(;iter != iter_end;iter++)
	{
		scene->PushEnemy( EFactory->MakeEnemy(*iter) );
	}
}

void Mediator::MakeUserBullet(EventList Elist)
{
	EventList::iterator iter = Elist.begin();
	EventList::iterator iter_end = Elist.end();
	for(;iter != iter_end;iter++)
	{
		scene->PushUBullet( PBFactory->MakeBullet(*iter) );
		delete *iter;
	}
}

void Mediator::MakeItem(EventList Elist)
{
	EventList::iterator iter = Elist.begin();
	EventList::iterator iter_end = Elist.end();
	for(;iter != iter_end;iter++)
	{
		scene->PushItem( IFactory->makeItem(*iter) );
		delete *iter;
	}
}

void Mediator::MakeEffect(EventList Elist)
{
	EventList::iterator iter = Elist.begin();
	EventList::iterator iter_end = Elist.end();
	for(;iter != iter_end; ++iter)
	{
		scene->PushEffect( EfFactory->EffectMake(*iter) );
		delete *iter;
	}
}

Mediator* Mediator::Instance()
{
	if(_p_this == NULL)
	{
		_p_this = new Mediator();
	}
	return _p_this;
}